﻿
namespace MyWebApp.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web.Ria;
    using System.Web.Ria.Data;
    using System.Web.DomainServices;
    using System.Data;
    using System.Web.DomainServices.LinqToEntities;


    // Implements application logic using the AdventureWorks_DataEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    [EnableClientAccess()]
    public class AdventureWorksDomainService : LinqToEntitiesDomainService<AdventureWorks_DataEntities>
    {

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.        

        // It is important to note that domain operations do not support method overloads. 
        // More concretely, if you want to have multiple query methods in the same domain service 
        // with different number of input parameters (e.g. GetEmployees() and GetEmployees(string)), 
        // you’ll need to name them differently.

        /// <summary>
        /// Query a list of employee
        /// </summary>
        /// <returns></returns>
        public IQueryable<Employee> GetEmployee()
        {
            return this.Context.Employee;
        }

        // convention based collection returning
        //public IQueryable<Employee> GetEmployees()
        //{
        //    return this.Context.Employee;
        //}

        // convention based singleton returning
        //public Employee GetEmployeeById(int empId)
        //{
        //    return this.Context.Employee.Single(p => p.EmployeeID == empId);
        //}

        // attribute based collection returning
        [Query]
        public IQueryable<Employee> ReturnAllEmployees()
        {
            return this.Context.Employee;
        }

        // attribute based singleton returning
        [Query(IsComposable = false)]
        public Employee GetEmployeeById(int empId)
        {
            return this.Context.Employee.Single(p => p.EmployeeID == empId);
        }


        // get employees with the specified marital status
        public IQueryable<Employee> GetEmployees(string maritalStatus)
        {
            return this.Context.Employee.Where(
                       e => e.MaritalStatus == maritalStatus);
        }

        /// <summary>
        /// Insert an employee
        /// </summary>
        /// <param name="employee"></param>
        public void InsertEmployee(Employee employee)
        {
            this.Context.AddToEmployee(employee);
        }

        /// <summary>
        /// Update an employee
        /// </summary>
        /// <param name="currentEmployee"></param>
        public void UpdateEmployee(Employee currentEmployee)
        {
            this.Context.AttachAsModified(currentEmployee, this.ChangeSet.GetOriginal(currentEmployee));
        }

        /// <summary>
        /// Delete an employee
        /// </summary>
        /// <param name="employee"></param>
        public void DeleteEmployee(Employee employee)
        {
            if ((employee.EntityState == EntityState.Detached))
            {
                this.Context.Attach(employee);
            }
            this.Context.DeleteObject(employee);
        }
    }
}


